package com.github.bluesbruce.websocket;

import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * WebSocket测试入口
 *
 * @author BBF
 */
@Controller
@RequestMapping("/")
public class WebSocketTestController {

  private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketTestController.class);

  @RequestMapping(value = {"/", "index"}, method = RequestMethod.GET)
  public String toStompWebSocket(HttpSession session, Model model) {
    LOGGER.info("跳转stomp页面，sessionId = {}", session.getId());
    // 这里封装一个登录的用户组参数，模拟进入通讯后的简单初始化
    model.addAttribute("groupId", "useGroupId");
    model.addAttribute("session_id", session.getId());
    model.addAttribute("endpoint", SocketConstants.SOCKET_ENDPOINT);
    model.addAttribute("socketPrefix", SocketConstants.SOCKET_PREFIX);
    model.addAttribute("socketUser", SocketConstants.SOCKET_USER);
    session.setAttribute("loginName", session.getId());
    return "/test/stomp";

  }
}
